package cn.it.shop.controller.wechat;

import java.io.File;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.it.shop.controller.BaseController;
import cn.it.shop.util.GlobalConstants;
import cn.it.shop.util.HttpPostUploadUtil;
import cn.it.shop.util.HttpUtils;
import cn.it.shop.util.JsonUtil;
import cn.it.shop.wechat.model.groupsend.OpenIDGroupSendText;
import cn.it.shop.wechat.model.groupsend.Text;
import net.sf.json.JSONObject;

/**
 * ClassName: debugController
 * 
 * @Description: 微信调试工具控制层
 * @author
 * @date
 */
@Controller
@Scope("prototype")
@RequestMapping("/debug")
public class DebugController extends BaseController{

	/***
	 * 请求相应接口
	 * @param url
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/debugging")
	public String jssdk(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		String result = "";
		try {
			//获取HttpServletRequest所有的参数 
			map = getRequest(request);
			//请求后台对应的方法
			result = getInterface(map);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println(JSONObject.fromObject(map).toString());
		//return JSONObject.fromObject(map).toString();
		return result;
	}

	
	//获取request所有参数方法
	public Map getRequest(HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		Enumeration paramNames = request.getParameterNames();  
		while (paramNames.hasMoreElements()) {  
			String paramName = (String) paramNames.nextElement();  

			String[] paramValues = request.getParameterValues(paramName);  
			if (paramValues.length == 1) {  
				String paramValue = paramValues[0];  
				if (paramValue.length() != 0) {  
					map.put(paramName, paramValue);  
				}  
			}  
		}  
	
		return map;
	}
	
	
	//根据参数调用不同的接口请求不同的方法
	public String getInterface(Map map){
		String urlKey = (String) map.get("url");//获取对应属性配置文件中的url
		 //根据不同的url key取出不同的参数,放到map中
		Map parameterMap = getParameterMap(urlKey,map);
		//
		String url = GlobalConstants.getInterfaceUrl(urlKey);//获取请求接口的url
		//如果是自定义菜单url,则把access_token追加到后面
		if(urlKey.equals("diyMenu")){
			url += map.get("access_token");
		}
		//获取请求方法类型
		String method = (String) map.get("method");
		//请求不同的方法
		String result = "";
		try {
			if(method.equals("GET")){
				//get请求,接收返回结果
				result = HttpUtils.sendGet(url, parameterMap);
			}else{
				result = HttpUtils.sendPostBuffer(url, parameterMap.get("body").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//转成jsonObject字符串 返回到前台显示
		return result;
	}
	
	//根据不同的url key取出不同的参数,放到map
	public Map getParameterMap(String urlKey,Map map){
		Map<String, String> parametMap = new HashMap<String, String>();
		
		switch(urlKey){
			
			case  "tokenUrl" :
				parametMap.put("grant_type", map.get("grant_type").toString());
				parametMap.put("appid", map.get("appid").toString());
				parametMap.put("secret", map.get("secret").toString());
				break;
			case "getUserList" :
				parametMap.put("access_token", map.get("access_token").toString());
				Object nextOpenid = map.get("next_openid");
				if(null != nextOpenid && !("".equals(nextOpenid))){
					parametMap.put("next_openid",nextOpenid.toString());
				}
				break;
			case "diyMenu" :
				parametMap.put("body", map.get("body").toString());
				break;
			default :
		}
		return parametMap;
	}
	
	
	//------------------------   群发文本消息开始  ----------------------------
	
	/****
	 * 群发文本消息
	 * @param request
	 * @return
	 */
	@RequestMapping("/sendMessage")
	public String sendMessage(HttpServletRequest request){
		//要发送的文本
		String text = request.getParameter("text");
		//获取用户openid列表
		List<Object> openIdList = audienceService.queryOpenidList();
		//url	
			//从全局变量工具类中取出access_token
			String accessToken = GlobalConstants.getInterfaceUrl("access_token");
			//获取  url getAllPrivateTemplate
			String getAllPrivateTemplate = GlobalConstants.getInterfaceUrl("openidListsendMessage");
			//替换掉 原url的accessToken字符串,用全局变量accessToken代替 
			getAllPrivateTemplate = getAllPrivateTemplate.replace("ACCESS_TOKEN", accessToken);
		//参数	
			//文本消息实体
			OpenIDGroupSendText openIDGroupSendText = new OpenIDGroupSendText();
			openIDGroupSendText.setTouser((List)openIdList);//强转类型
			openIDGroupSendText.setText(new Text(text));//设置内容文本
			openIDGroupSendText.setMsgtype("text");
			//总体的一个josnobject
			JSONObject jsonparams = JSONObject.fromObject(openIDGroupSendText);
			System.out.println(getAllPrivateTemplate);
		//调用群发接口
			try {
				String result = HttpUtils.sendPostBuffer(getAllPrivateTemplate, jsonparams.toString());
				System.out.println(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		return "";
	}
	
	//------------------------   群发文本消息结束  ----------------------------
	
	
	
	
	
	
}
